package controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.CASE_GOODS_LEND;
import model.User_list;

import com.et.ar.exception.ActiveRecordException;
import com.et.mvc.JsonView;

/**
 * CasePropertyReturnController.java
 * @author crab
 * 
 * remark:物品归还
 */
public class CasePropertyReturnController extends ApplicationController {

	private static final String enter = " \n";
	public JsonView getLendedGoods(int rows, int page) throws ActiveRecordException{
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul.getUser_company();
		StringBuilder condition = new StringBuilder();
		condition.append("1=1 "); 
		condition.append(" and tcgl.operate_company=").append(company_id).append(enter); 
		String ajmc = request.getParameter("AJMC");//案件名称
		if(ajmc!=null && ajmc.length()>0)
			buildCond(condition,"%"+ajmc+"%","TCGI.CASE_NAME","like");
		String wpmc = request.getParameter("WPMC");//物品名称
		if(wpmc!=null && wpmc.length()>0)
			buildCond(condition,"%"+wpmc+"%","TCGI.GOODS_NAME","like");
		String badw = request.getParameter("BADW");//办案单位
		if(badw!=null && badw.length()>0)
			buildCond(condition,"%"+badw+"%","TCGI.CASE_COMPANY","like");
		String bar = request.getParameter("BAR");//办案人员
		if(bar!=null && bar.length()>0)
			buildCond(condition,"%"+bar+"%","TCGI.CASE_MAN","like");
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT TCGI.GOODS_ID,").append(enter);
		sql.append("       TCGI.GOODS_TCODE t_code,").append(enter);
		sql.append("       TCGI.GOODS_NAME,").append(enter);
		sql.append("       TCGI.CASE_NAME,").append(enter);
		sql.append("       TCGI.CASE_NUM,").append(enter);
		sql.append("       TCGL.LEND_COMPANY,").append(enter);
		sql.append("       TCGL.LEND_PERSON,").append(enter);
		sql.append("       tcgl.lend_date").append(enter);
		sql.append("  FROM T_CASE_GOODS_LEND TCGL").append(enter);
		sql.append(" INNER JOIN T_CASE_GOODS_INFO TCGI ON TCGL.GOODS_ID = TCGI.GOODS_ID").append(enter);
		sql.append("                                  AND TCGI.GOODS_TCODE = TCGL.T_CODE").append(enter);
		sql.append(" where ").append(condition.toString());
		sql.append("ORDER BY tcgi.case_num");
		
		StringBuilder countsql = new StringBuilder();
		countsql.append("select count(*) count").append(enter);
		countsql.append("  FROM T_CASE_GOODS_LEND TCGL").append(enter);
		countsql.append(" INNER JOIN T_CASE_GOODS_INFO TCGI ON TCGL.GOODS_ID = TCGI.GOODS_ID").append(enter);
		countsql.append("                                  AND TCGI.GOODS_TCODE = TCGL.T_CODE").append(enter);
		countsql.append(" where ").append(condition.toString());
		List<Map<String,Object>> count_map = CASE_GOODS_LEND.getResultMap(CASE_GOODS_LEND.class, countsql.toString(), null, "", 0,Integer.MAX_VALUE);
		Long total = count_map.size()>0?Long.parseLong(count_map.get(0).get("count").toString()):1;
		List<Map<String,Object>> lendList = CASE_GOODS_LEND.getResultMap(CASE_GOODS_LEND.class, sql.toString(), null, "", rows, (page - 1) * rows);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);//数据总数
		result.put("rows", lendList);
		return new JsonView(result);
	}
}
